package com.zyh.homework.test3;

import com.zyh.homework.test3.domain.Student;
import com.zyh.homework.test3.domain.StudentEntry;

import java.util.*;

/**
 * @Author zyh
 * @Date 2024/8/1 20:39
 * @Description
 */

public class ListToMap {
    public void listToMap() {
        Student student1 = new Student(1, "zyh", 20, "男");
        Student student2 = new Student(2, "cxk", 21, "男");
        Student student3 = new Student(3, "dz", 22, "女");
        Student student4 = new Student(4, "jyh", 90, "女");
        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        for (Student stu : list) {
            System.out.println("学生编号是：" + stu.getId() + "\t学生姓名是：" + stu.getName() + "\t学生年龄是：" + stu.getAge() + "\t学生性别是：" + stu.getSex());
        }
        Map<Integer,Student> map=new HashMap<>();
        map.put(1,list.get(0));
        map.put(2,list.get(1));
        map.put(3,list.get(2));
        map.put(4,list.get(3));
        Set<Map.Entry<Integer, Student>> entries = map.entrySet();
        for (Map.Entry<Integer, Student> entry : entries) {
            System.out.println(entry.getKey()+"---"+entry.getValue());
        }
    }

    public void mapToList() {
        StudentEntry student1 = new StudentEntry("zyh", 20, "男");
        StudentEntry student2 = new StudentEntry("cxk", 21, "男");
        StudentEntry student3 = new StudentEntry("dz", 22, "女");
        StudentEntry student4 = new StudentEntry("jyh", 90, "女");
        Map<Integer, StudentEntry> map = new HashMap();
        map.put(1,student1);
        map.put(2,student2);
        map.put(3,student3);
        map.put(4,student4);
        List<StudentEntry> list=new ArrayList<>();
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            StudentEntry value = map.get(key);
            list.add(value);
        }
        for (StudentEntry stu : list) {
            System.out.println("学生姓名是：" + stu.getName() + "\t学生年龄是：" + stu.getAge() + "\t学生性别是：" + stu.getSex());
        }
    }
}
